package cn.jjxx.shiro.session.core;

import java.io.Serializable;

import org.apache.shiro.session.Session;

import cn.jjxx.modules.sys.entity.User;
import cn.jjxx.modules.sys.utils.UserUtils;
import cn.jjxx.shiro.session.config.RedisConfig;
import cn.jjxx.shiro.session.config.RedisUtil;

/**
 * 操作redis的工具类
 *@Title ShiroSessionRedisUtil.java
 *@description TODO
 *@time 2020年7月16日 下午12:32:59
 *@author YAOXIONG
 *@version 1.0
 *
 */
public class ShiroSessionRedisUtil { 
	
	
	public static Session getSession(Serializable sessionId){
		Session session = null;
		byte[] bytes = RedisUtil.getObject(ShiroSessionRedisConstant.SHIROSESSION_REDIS_DB,
				(ShiroSessionRedisConstant.SHIROSESSION_REDIS_PREFIX+sessionId.toString()).getBytes(),
				ShiroSessionRedisConstant.SHIROSESSION_REDIS_EXTIRETIME);
		if(bytes != null && bytes.length > 0){
			session = ShiroSessionConvertUtil.byteToSession(bytes);
		}
		return session;
	}
	public static void updateSession(Session session){ 
		RedisUtil.updateObject(ShiroSessionRedisConstant.SHIROSESSION_REDIS_DB,
				(ShiroSessionRedisConstant.SHIROSESSION_REDIS_PREFIX+session.getId().toString()).getBytes(),
				ShiroSessionConvertUtil.sessionToByte(session),
				ShiroSessionRedisConstant.SHIROSESSION_REDIS_EXTIRETIME);
		//也要更新token
		/*User user = (User)session.getAttribute(Const.SESSION_USER);
		if(null != user){
			RedisUtil.updateString(SHIROSESSION_REDIS_DB,ShiroSessionRedisConstant.SSOTOKEN_REDIS_PREFIX+user.getUSERNAME(),ShiroSessionRedisConstant.SHIROSESSION_REDIS_EXTIRETIME);
		}*/
	}

	public static void deleteSession(Session session){
		RedisUtil.delString(ShiroSessionRedisConstant.SHIROSESSION_REDIS_DB,
				ShiroSessionRedisConstant.SHIROSESSION_REDIS_PREFIX+session.getId().toString());
		
		RedisUtil.delString(ShiroSessionRedisConstant.SHIROSESSION_REDIS_DB,
				ShiroSessionRedisConstant.SSO_SHIROSESSION_REDIS_PREFIX+session.getId().toString());
		
		RedisUtil.delString(ShiroSessionRedisConstant.SHIROSESSION_REDIS_DB,
				ShiroSessionRedisConstant.PWD_SESSION+session.getId().toString());
				
		RedisUtil.delString(ShiroSessionRedisConstant.SHIROSESSION_REDIS_DB,
				ShiroSessionRedisConstant.MAIN_SYSTEM_TOKEN+session.getId().toString());
		//也要删除token
		/*//User user = (User)session.getAttribute(Const.SESSION_USER);
		//if(null != user){ 
			RedisUtil.delString(ShiroSessionRedisConstant.SHIROSESSION_REDIS_DB,
					ShiroSessionRedisConstant.SSOTOKEN_REDIS_PREFIX+user.getUSERNAME());
		}*/
	}
	/**
	 * 创建单点登录信息
	*@Title ShiroSessionRedisUtil.java
	*@description TODO
	*@time 2020年10月20日 下午3:18:57
	*@author YAOXIONG
	*@version 1.0
	*
	 */
	public static void setSSOCreateSession(Session session) {
		/*try {
			//登录成功之后保存数据
			String key = ShiroSessionRedisConstant.MAIN_SYSTEM_TOKEN + session.getId().toString();
			String value = session.getId().toString();
			RedisUtil.setString(ShiroSessionRedisConstant.SHIROSESSION_REDIS_DB,
					key,value,
					ShiroSessionRedisConstant.SHIROSESSION_REDIS_EXTIRETIME);
		} catch (Exception e) {
			// TODO: handle exception
		} */
		
		try {
			RedisUtil.updateObject(ShiroSessionRedisConstant.SHIROSESSION_REDIS_DB,
					(ShiroSessionRedisConstant.MAIN_SYSTEM_TOKEN+session.getId().toString()).getBytes(),
					ShiroSessionConvertUtil.sessionToByte(session),
					ShiroSessionRedisConstant.SHIROSESSION_REDIS_EXTIRETIME);
		} catch (Exception e) {
			// TODO: handle exception
		} 
	}
	
}
